package com.ruoyi.web.controller.TiebaChatUser.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.web.controller.TiebaChatUser.domain.TiebaChatUser;
import com.ruoyi.web.controller.TiebaChatUser.mapper.TiebaChatUserMapper;
import com.ruoyi.web.controller.TiebaChatUser.service.ITiebaChatUserService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 聊天对象Service业务层处理
 *
 * @author yong
 * @date 2025-03-09
 */
@Service
public class TiebaChatUserServiceImpl extends ServiceImpl<TiebaChatUserMapper, TiebaChatUser> implements ITiebaChatUserService {

    @Autowired
    private TiebaChatUserMapper tiebaChatUserMapper;

    /**
     * 查询聊天对象列表
     *
     * @param tiebaChatUser 聊天对象
     * @return 聊天对象
     */
    @Override
    public List<TiebaChatUser> selectTiebaChatUserList(TiebaChatUser tiebaChatUser) {
        List<TiebaChatUser> tiebaChatUserList = tiebaChatUserMapper.selectList(buildQueryWrapper(tiebaChatUser));
        return tiebaChatUserList;
    }

    @Override
    public List<Long> selectUserIdsByCondition(TiebaChatUser condition) {
        LambdaQueryWrapper<TiebaChatUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TiebaChatUser::getChatUserId, condition.getUserId());
        return tiebaChatUserMapper.selectList(queryWrapper).stream()
                .map(TiebaChatUser::getChatUserId)
                .collect(Collectors.toList());
    }


    private LambdaQueryWrapper<TiebaChatUser> buildQueryWrapper(TiebaChatUser query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<TiebaChatUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getUserId() != null, TiebaChatUser::getUserId, query.getUserId());
//        lqw.eq(query.getDeptId() != null, TiebaChatUser::getDeptId, query.getDeptId());
//        lqw.like(StringUtils.isNotBlank(query.getUsername()), TiebaChatUser::getUsername, query.getUsername());
//        lqw.eq(StringUtils.isNotBlank(query.getAvatarUrl()), TiebaChatUser::getAvatarUrl, query.getAvatarUrl());
//        lqw.eq(StringUtils.isNotBlank(query.getLastMessage()), TiebaChatUser::getLastMessage, query.getLastMessage());
//        lqw.eq(query.getLastMessageTime() != null, TiebaChatUser::getLastMessageTime, query.getLastMessageTime());
        lqw.orderByDesc(TiebaChatUser::getCreateTime);
//        lqw.eq(query.getChatUserId() != null, TiebaChatUser::getChatUserId, query.getChatUserId());
        return lqw;
    }

}
